package com.dj.mingkong.common.infrastructure.base.dto.rest;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zenglonghui
 * @date: 2022/5/16 18:17
 */
public class RestResponse<T> implements Serializable {
    public static final String SUCCESS_CODE = "0";
    public static final String FAILURE_CODE = "1";
    public static final String ERROR_CODE = "500";
    public static final String NOT_FOUND = "404";
    public static final String BAD_REQUEST = "400";
    public static final String UNAUTHORIZED = "401";
    public static final String FORBIDDEN = "403";
    public static final String SUCCESS_MSG = "success";
    public static final String ERROR_MSG = "Internal system error";
    public static final String FAILURE_MSG = "failure";
    public static final String NOT_FOUND_MSG = "not found";
    public static final String BAD_REQUEST_MSG = "The API request is invalid";
    public static final String UNAUTHORIZED_MSG = "Access is denied due to invalid credentials";
    public static final String FORBIDDEN_MSG = "Access to this resource on the server is denied";
    protected String resultCode;
    protected String resultMsg;
    protected T data;
    protected transient String exceptCauseIp;
    protected transient String exceptCauseApp;
    protected transient String exceptClass;
    public static final RestResponse<Void> VOID = new RestResponse((Object) null);
    public static final RestResponse<Boolean> TRUE;
    public static final RestResponse<Boolean> FALSE;

    public RestResponse() {
        this((T) null);
    }

    public RestResponse(T data) {
        this.resultCode = "0";
        this.resultMsg = "success";
        this.data = data;
    }

    public RestResponse(String resultCode, String resultMessage) {
        this.resultCode = resultCode;
        this.resultMsg = resultMessage;
    }

    public RestResponse(String resultCode, String resultMsg, T data) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.data = data;
    }

    public static <T> RestResponse<T> buildSuccess() {
        return new RestResponse();
    }

    public static <T> RestResponse<T> build400() {
        return new RestResponse("400", "The API request is invalid");
    }

    public static <T> RestResponse<T> build401() {
        return new RestResponse("401", "Access is denied due to invalid credentials");
    }

    public static <T> RestResponse<T> build401(String resultMsg) {
        return new RestResponse("401", resultMsg);
    }

    public static <T> RestResponse<T> build403() {
        return new RestResponse("403", "Access to this resource on the server is denied");
    }

    public static <T> RestResponse<T> build404() {
        return new RestResponse("404", "not found");
    }

    public static <T> RestResponse<T> build500() {
        return new RestResponse("500", "Internal system error");
    }

    public static <T> RestResponse<T> build500(String message) {
        return new RestResponse("500", message);
    }

    public static <T> RestResponse<T> build400(String errorMsg) {
        return new RestResponse("400", errorMsg);
    }

    public boolean isSuccess() {
        return this.resultCode != null && this.resultCode.length() != 0 && "0".equals(this.resultCode);
    }

    public static <T> RestResponse<T> error(String errorMessage) {
        return new RestResponse("500", errorMessage);
    }

    public static <T> RestResponse<T> error(String resultCode, String errorMessage) {
        return new RestResponse(resultCode, errorMessage);
    }

    public static <T> RestResponse<T> success(T data) {
        return new RestResponse("0", "success", data);
    }

    public static <T> RestResponse<T> success(String code, String msg, T data) {
        return new RestResponse(code, msg, data);
    }

    public static <T> RestResponse<T> fail(String failMessage) {
        return new RestResponse("1", failMessage);
    }

    public static RestResponse<Long> createLong(Long value) {
        return new RestResponse(value);
    }

    public static RestResponse<Short> createShort(Short value) {
        return new RestResponse(value);
    }

    public static RestResponse<Integer> createInteger(Integer value) {
        return new RestResponse(value);
    }

    public static RestResponse<Float> createFloat(Float value) {
        return new RestResponse(value);
    }

    public static RestResponse<Double> createDouble(Double value) {
        return new RestResponse(value);
    }

    public static RestResponse<BigDecimal> createBigDecimal(BigDecimal value) {
        return new RestResponse(value);
    }

    public static RestResponse<Object> createObject(Object obj) {
        return new RestResponse(obj);
    }

    public String getExceptCauseIp() {
        return this.exceptCauseIp;
    }

    public void setExceptCauseIp(String exceptCauseIp) {
        this.exceptCauseIp = exceptCauseIp;
    }

    public String getExceptCauseApp() {
        return this.exceptCauseApp;
    }

    public void setExceptCauseApp(String exceptCauseApp) {
        this.exceptCauseApp = exceptCauseApp;
    }

    public String getExceptClass() {
        return this.exceptClass;
    }

    public void setExceptClass(String exceptClass) {
        this.exceptClass = exceptClass;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getResultCode() {
        return this.resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return this.resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap(3);
        map.put("resultCode", this.resultCode);
        map.put("resultMsg", this.resultMsg);
        map.put("data", this.data);
        return map;
    }

    public void checkResponse() {
        if (!"0".equals(this.resultCode)) {
            throw new IllegalStateException("业务异常：" + this.resultMsg);
        }
    }

    @Override
    public String toString() {
        return "RestResponse [resultCode=" + this.resultCode + ", resultMsg=" + this.resultMsg + ", data=" + this.data + "]";
    }

    static {
        TRUE = new RestResponse("0", "success", Boolean.TRUE);
        FALSE = new RestResponse("1", "failure", Boolean.FALSE);
    }
}
